/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.neuralstudio.utils;

import java.util.ArrayList;
import java.util.List;
import org.jane.utils.AbstractDataset;
import org.jane.utils.SupervisedDataset;
import org.jane.utils.UnsupervisedDataset;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author william
 */
public class DatasetSaxHandler extends DefaultHandler{
    private AbstractDataset dataset;
    private List<Float> inputAux;
    private List<Float> outputAux;
    private boolean input;
    private boolean value;
    private boolean output;
    private String nombre;
    private boolean row;
    /*
     *  <dataset type = "supervised">
            <row>
                <input>
                    <value></value>
                </input>
     *          <output>
                    <value></value>
                </output>
            </row>
        </dataset>
     *
     * <dataset type = "unsupervised">
            <row>
                <value></value>
            </row>
        </dataset>
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs) {
        if ("dataset".equals(qName)) {
            this.inputAux = new ArrayList<Float>();
            if ("supervised".equals(attrs.getValue("type"))) {
                this.dataset = new SupervisedDataset();
                this.outputAux = new ArrayList<Float>();
            }else{
                this.dataset = new UnsupervisedDataset();
                this.outputAux = new ArrayList<Float>();
            }
            this.nombre = attrs.getValue("nombre");
        }
        if ("input".equals(qName)) {
            this.input = true;
        }
        if ("output".equals(qName)) {
            this.output = true;
        }
        if ("value".equals(qName)) {
            this.value = true;
        }
        if ("row".equals(qName)) {
            this.row = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("input".equals(qName)) {
            this.input = false;
        }
        if ("output".equals(qName)) {
            this.output = false;
        }
        if ("value".equals(qName)) {
            this.value = false;
        }
        if ("row".equals(qName)) {
            if (this.dataset instanceof SupervisedDataset) {
                List l = new ArrayList<List<Float>>(2);
                l.add(inputAux);
                l.add(outputAux);
                this.outputAux = new ArrayList<Float>();
                ((SupervisedDataset)dataset).addDataRow(l);
            }else{
                ((UnsupervisedDataset)dataset).addDataRow(inputAux);
            }
            this.inputAux = new ArrayList<Float>();
        }
        if ("dataset".equals(qName)) {
            //TrainingDataManager.getInstance().addDataset(this.nombre, dataset);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (row && value && dataset instanceof UnsupervisedDataset) {
            this.inputAux.add(Float.parseFloat(new String(ch,start,length)));
        }
        if (input && value) {
            this.inputAux.add(Float.parseFloat(new String(ch,start,length)));
        }
        if (output && value) {
            this.outputAux.add(Float.parseFloat(new String(ch,start,length)));
        }
    }

    public AbstractDataset getDataset() {
        return dataset;
    }

    public String getNombre() {
        return nombre;
    }

}
